<template>
	<view class="content">
		<view v-for="(item, index) in list" :key="index">
			<button @click="launchApp(item)">打开{{ item.name }}</button>
		</view>
		<view>当前经度{{ count }}：{{ longitude }}</view>
		<view>当前纬度{{ count }}：{{ latitude }}</view>

		<navigator url="../../subpages/test">111111111111111111111</navigator>
	</view>
</template>

<script>
export default {
	data() {
		return {
			count: 1,
			longitude: '',
			latitude: '',
			title: 'Hello',
			list: [
				{
					name: '支付宝',
					pname: 'com.eg.android.AlipayGphone',
					scheme: 'alipay://'
				},
				{
					name: '淘宝',
					pname: 'com.taobao.taobao',
					scheme: 'taobao://'
				},
				{
					name: 'QQ',
					pname: 'com.tencent.mobileqq',
					scheme: 'mqq://'
				},
				{
					name: '微信',
					pname: 'com.tencent.mm',
					scheme: 'weixin://'
				},
				{
					name: '京东',
					pname: 'com.jingdong.app.mall',
					scheme: 'openApp.jdMobile://'
				},
				{
					name: '新浪微博',
					pname: 'com.sina.weibo',
					scheme: 'sinaweibo://'
				},
				{
					name: '今日头条',
					pname: 'com.ss.android.article.news',
					scheme: 'snssdk141://'
				}
			]
		};
	},

	onLoad() {
		// #ifdef APP-PLUS
		// 获取是否开启定位
		let system = uni.getSystemInfoSync(); // 获取系统信息
		if (system.platform === 'android') {
			// 判断平台
			var context = plus.android.importClass('android.content.Context');
			var locationManager = plus.android.importClass('android.location.LocationManager');
			var main = plus.android.runtimeMainActivity();
			var mainSvr = main.getSystemService(context.LOCATION_SERVICE);
			if (!mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER)) {
				var main = plus.android.runtimeMainActivity();
				var Intent = plus.android.importClass('android.content.Intent');
				var Settings = plus.android.importClass('android.provider.Settings');
				var intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
				main.startActivity(intent); // 打开系统设置GPS服务页面
			}
		} else if (system.platform === 'ios') {
			var cllocationManger = plus.ios.import('CLLocationManager');
			var enable = cllocationManger.locationServicesEnabled();
			var status = cllocationManger.authorizationStatus();
			plus.ios.deleteObject(cllocationManger);
			console.log('手机系统的定位没有打开');
			uni.showModal({
				title: '提示',
				content: '请打开定位服务功能',
				showCancel: false, // 不显示取消按钮
				success() {
					var UIApplication = plus.ios.import('UIApplication');
					var application2 = UIApplication.sharedApplication();
					var NSURL2 = plus.ios.import('NSURL');
					var setting2 = NSURL2.URLWithString('App-Prefs:root=Privacy&path=LOCATION');
					application2.openURL(setting2);
					plus.ios.deleteObject(setting2);
					plus.ios.deleteObject(NSURL2);
					plus.ios.deleteObject(application2);
				}
			});
		}
		// #endif

		setInterval(() => {
			this.count++;
			this.getLocation();
		}, 2000);
	},
	onPullDownRefresh() {
		this.$router.go(0);
		//刷新数据之后停止刷新效果
		uni.stopPullDownRefresh();
	},

	methods: {
		getLocation() {
			let that = this;
			uni.getLocation({
				type: 'wgs84',
				success: res => {
					console.log('当前位置的经度：' + res.longitude);
					console.log('当前位置的纬度：' + res.latitude);
					console.log('当前位置的速度：' + res.speed);
					console.log('当前位置的精确度：' + res.accuracy);
					that.longitude = res.longitude;
					that.latitude = res.longitude;
				}
			});
		},
		launchApp(e) {
			let _this = this;
			// 判断平台
			// #ifdef APP-PLUS
			if (plus.os.name == 'Android') {
				plus.runtime.launchApplication(
					{
						pname: e.pname
					},
					function(e) {
						_this.$u.toast('未发现该应用');
						console.log('Open system default browser failed: ' + e.message);
					}
				);
			} else if (plus.os.name == 'iOS') {
				plus.runtime.launchApplication({ action: e.scheme }, function(e) {
					console.log('Open system default browser failed: ' + e.message);
				});
			}
			// #endif
		}
	}
};
</script>
<style lang="scss" scoped></style>
